﻿using Newtonsoft.Json;
using Rezene.Web.Business.Amazon.Entities;

namespace Rezene.Web.Business.Amazon
{
    public class AmazonFactory : IAmazonFactory
    {
        private string GetNameFromPath(string path)
        {
            var lastSlash = path.LastIndexOf("/");
            if(lastSlash >= 0 && lastSlash != path.Length)
            {
                return path.Substring(lastSlash + 1, path.Length - lastSlash - 1);
            }
            return path;
        }

        public S3BaseObject CreateDefaultObjectFromPath(string bucketName, string path)      
        {
            if (path.EndsWith("/"))
            {
                path = path.Substring(0, path.Length - 1);
            }

            var name = GetNameFromPath(path);
            if (!string.IsNullOrEmpty(name))
            {
                path = path.Replace(name, "");
            }

            return new S3BaseObject()
            {
                Name = name,
                Path = path,
                Bucket = bucketName
            };
        }

  

        public S3Gallery CreateGallery(string bucketName, string name, string path, string title, string description, bool show)
        {
            return new S3Gallery()
            {
                Bucket = bucketName,
                Name = name,
                Path = path,
                Description = description,
                Title = title,
                Show = show
            };
        }

        public S3Gallery CreateGalleryFromJson(string json)
        {
            return JsonConvert.DeserializeObject<S3Gallery>(json);
        }

        public S3PhotoObject CreatePhotoObjectFromJson(string json)
        {
            return JsonConvert.DeserializeObject<S3PhotoObject>(json);
        }

        public S3PhotoObject CreatePhotoObject(string bucketName, string name, string path, string title, string description)
        {
            return new S3PhotoObject()
            {
                Bucket = bucketName,
                Name = name,
                Path = path,
                Title = title,
                Description = description
            };
        }
    }
}